package ms.platform.email;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

/**
 * 邮件发送服务
 * 
 * @author Kevin Deng
 *
 */
@Component
public class MailService implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -592023235638216196L;
	
	@Resource(name="mailConfig")
	private MailConfig mailConfig;

	/*private static final String HOST = "smtp.126.com";
	private static final Integer PORT = 25;
	private static final String USERNAME = "mosheo_deng@126.com";
	private static final String PASSWORD = "niao4fandjb";
	private static final String EMAILFORM = "mosheo_deng@126.com";*/
//	private JavaMailSenderImpl mailSender = createMailSender();

	/**
	 * 邮件发送器
	 *
	 * @return 配置好的工具
	 */
	private JavaMailSenderImpl createMailSender() {
		JavaMailSenderImpl sender = new JavaMailSenderImpl();
		sender.setHost(mailConfig.getHost());
		sender.setPort(mailConfig.getPort());
		sender.setUsername(mailConfig.getUserName());
		sender.setPassword(mailConfig.getPassword());
		sender.setDefaultEncoding("Utf-8");
		Properties p = new Properties();
		p.setProperty("mail.smtp.timeout", mailConfig.getTimeOut().toString());
		p.setProperty("mail.smtp.auth", "true");//开启认证  
        p.setProperty("mail.debug", "true");//启用调试  
        p.setProperty("mail.smtp.port", Integer.toString(mailConfig.getPort()));//设置端口  
        p.setProperty("mail.smtp.socketFactory.port", Integer.toString(mailConfig.getPort()));//设置ssl端口  
        p.setProperty("mail.smtp.socketFactory.fallback", "false");  
        p.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");  
		sender.setJavaMailProperties(p);
		return sender;
	}

	/**
	 * 发送邮件
	 *
	 * @param to
	 *            接受人
	 * @param subject
	 *            主题
	 * @param html
	 *            发送内容
	 * @throws MessagingException
	 *             异常
	 * @throws UnsupportedEncodingException
	 *             异常
	 */
	public void sendHtmlMail(String to, String subject, String html)
			throws MessagingException, UnsupportedEncodingException {
		JavaMailSenderImpl mailSender = createMailSender();
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		// 设置utf-8或GBK编码，否则邮件会有乱码
		MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
		messageHelper.setFrom(mailConfig.getMailFrom(), "爱机金型进度管理系统");
		messageHelper.setTo(to);
		messageHelper.setSubject(subject);
		messageHelper.setText(html, true);
		mailSender.send(mimeMessage);
	}

	/**
	 * 发送邮件
	 *
	 * @param mailMap
	 *            收件人与邮件内容集合
	 * @throws MessagingException
	 *             异常
	 */
	public void sendHtmlMail(Map<String, String> mailMap) throws MessagingException {
		JavaMailSenderImpl mailSender = createMailSender();
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		// 设置utf-8编码
		MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
		messageHelper.setFrom(mailConfig.getMailFrom());
		Iterator<String> iterator = mailMap.keySet().iterator();
		while (iterator.hasNext()) {
			messageHelper.setTo(iterator.next());
			// messageHelper.setSubject(subject);
			messageHelper.setText(mailMap.get(iterator.next()), true);
			mailSender.send(mimeMessage);
		}
	}

	public MailConfig getMailConfig() {
		return mailConfig;
	}

	public void setMailConfig(MailConfig mailConfig) {
		this.mailConfig = mailConfig;
	}
	
	
}
